using UnityEngine;
using Spine;
using Spine.Unity;
/// <summary>
/// 测试皮肤切换
/// </summary>
public class SpineOutfitChanger : MonoBehaviour {
    [Header("Spine组件")]
    public SkeletonAnimation skeletonAnimation;

    [Header("皮肤部件")]
    public string[] bodySkins;
    public string[] hairSkins;
    public string[] clothesSkins;
    public string[] weaponSkins;

    int currentBodyIndex = 0;
    int currentHairIndex = 0;
    int currentClothesIndex = 0;
    int currentWeaponIndex = 0;

    void Start() {
        if (skeletonAnimation == null)
            skeletonAnimation = GetComponent<SkeletonAnimation>();

        ApplyOutfit();
    }

    // 应用当前选择的皮肤组合
    public void ApplyOutfit() {
        if (skeletonAnimation == null)
            return;

        // 获取骨骼和当前皮肤
        Skeleton skeleton = skeletonAnimation.Skeleton;
        Skin combinedSkin = new Skin("combined-skin");

        // 添加默认皮肤作为基础（如果有）
        Skin defaultSkin = skeleton.Data.DefaultSkin;
        if (defaultSkin != null)
            combinedSkin.AddSkin(defaultSkin);

        // 添加各个部位的皮肤
        AddSkinPart(combinedSkin, bodySkins, currentBodyIndex);
        AddSkinPart(combinedSkin, hairSkins, currentHairIndex);
        AddSkinPart(combinedSkin, clothesSkins, currentClothesIndex);
        AddSkinPart(combinedSkin, weaponSkins, currentWeaponIndex);

        // 应用组合皮肤
        skeleton.SetSkin(combinedSkin);
        skeleton.SetSlotsToSetupPose();
        skeletonAnimation.AnimationState.Apply(skeleton);
    }

    // 添加皮肤部件
    private void AddSkinPart(Skin combinedSkin, string[] skinList, int index) {
        if (skinList == null || skinList.Length == 0 || index < 0 || index >= skinList.Length)
            return;

        Skin partSkin = skeletonAnimation.Skeleton.Data.FindSkin(skinList[index]);
        if (partSkin != null)
            combinedSkin.AddSkin(partSkin);
    }

    // UI控制方法
    public void NextBody() {
        if (bodySkins.Length == 0)
            return;
        currentBodyIndex = (currentBodyIndex + 1) % bodySkins.Length;
        ApplyOutfit();
    }

    public void PreviousBody() {
        if (bodySkins.Length == 0)
            return;
        currentBodyIndex = (currentBodyIndex - 1 + bodySkins.Length) % bodySkins.Length;
        ApplyOutfit();
    }

    public void NextHair() {
        if (hairSkins.Length == 0)
            return;
        currentHairIndex = (currentHairIndex + 1) % hairSkins.Length;
        ApplyOutfit();
    }

    public void PreviousHair() {
        if (hairSkins.Length == 0)
            return;
        currentHairIndex = (currentHairIndex - 1 + hairSkins.Length) % hairSkins.Length;
        ApplyOutfit();
    }

    // 其他部位的控制方法类似...

    // 随机换装
    public void RandomizeOutfit() {
        if (bodySkins.Length > 0)
            currentBodyIndex = Random.Range(0, bodySkins.Length);
        if (hairSkins.Length > 0)
            currentHairIndex = Random.Range(0, hairSkins.Length);
        if (clothesSkins.Length > 0)
            currentClothesIndex = Random.Range(0, clothesSkins.Length);
        if (weaponSkins.Length > 0)
            currentWeaponIndex = Random.Range(0, weaponSkins.Length);

        ApplyOutfit();
    }
}
